// 全局消息提示、通知设置
export const messagePrompt = {
  msgError (msg) {
    this.$message({
      message: msg,
      type: 'error',
      duration: 3000
    })
  },
  msgSuccess (msg) {
    this.$message({
      dangerouslyUseHTMLString: true,
      message: msg,
      type: 'success',
      duration: 3000
    })
  },
  msgConfirm (msg, object = {}) {
    let obj = Object.assign({
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      lockScroll: true
    }, object)
    return this.$msgbox.confirm(msg, '温馨提示', obj)
  },
  msgAlert (msg, title, object = {}) {
    let obj = Object.assign({
      confirmButtonText: '确定'
    }, object)
    return this.$msgbox.alert(msg, title, obj)
  }
}

export const checkFunction = {
  rangeValid (start, end, isRequest, type = 'date', days) {
    // start:开始时间
    // end:结束时间
    // isRequest:是否必填
    if (isRequest || start || end) {
      if (!start) {
        return type === 'date' ? '请选择开始时间' : '请输入最低金额'
      }
      if (!end) {
        return type === 'date' ? '请选择结束时间' : '请输入最高金额'
      }
    }
    if (start && end) {
      switch (type) {
        case 'date':
          let starttime = new Date(start).getTime()
          let endttime = new Date(end).getTime()
          let nowtime = new Date().getTime()
          let datetime = 86400000
          if (starttime > endttime) {
            return '开始时间不能大于结束时间'
          }
          if (days) {
            if (nowtime - starttime > datetime * days) {
              return '只能查询最近' + days + '天记录'
            }
          }
          break
        case 'money':
          if (parseFloat(start) - parseFloat(end) >= 0) {
            return '最低金额不能大于等于最高金额'
          }
          break
      }
    }
    return false
  }
}
export function camelToKebab (str) {
  return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}
export default {
  install (Vue, options) {
    // 添加消息功能方法
    Object.assign(Vue.prototype, messagePrompt)
    Object.assign(Vue.prototype, soundMessage)
    Object.assign(Vue.prototype, checkFunction)
    let uid = 0
    Vue.mixin({
      beforeCreate () {
        // 添加组件唯一标识
        this.$uid = uid.toString()
        uid += 1
      }
    })
  }
}
export function dateFormat (time, format = 'yyyy-MM-dd HH:mm:ss') { // time 为毫秒或者 直接new Date()， format 为格式
  if (!time) return
  var t = new Date(time)
  if (!t) return
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i
  }
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear())
      case 'MM':
        return tf(t.getMonth() + 1)
      case 'dd':
        return tf(t.getDate())
      case 'HH':
        return tf(t.getHours())
      case 'mm':
        return tf(t.getMinutes())
      case 'ss':
        return tf(t.getSeconds())
    }
  })
}

export const soundMessage = {
  myInstance: null, // 当前音乐实例
  musicObj: '',
  fileload: null, // loadHandler事件注册
  _this: '', // vue 组件
  playSound (soundID, obj) {
    soundMessage._this = this
    this.$store.state.musicLoading = true
    if (!window.createjs.musicSource) {
      window.createjs.musicSource = {}
      window.createjs.LoadItem.crossOrigin = true
    }
    let currSound = window.createjs.musicSource[soundID]
    if (!currSound || typeof currSound === 'undefined') {
      if (obj.url) {
        window.createjs.Sound.alternateExtensions = ['mp3']
        soundMessage.musicObj = obj
        if (!soundMessage.fileload) soundMessage.fileload = window.createjs.Sound.on('fileload', loadHandler)
        let instance = window.createjs.Sound.registerSound(obj.url, soundID)
        if (instance === true) {
          mergeObj(obj.url, soundID)
          if (obj.url) delete obj.url
          window.createjs.musicSource[soundID].play(obj)
          soundMessage.myInstance = window.createjs.musicSource[soundID]
          soundMessage.myInstance.on('complete', handleComplete)
        }
      }
      return
    }
    if (currSound.playState === 'playSucceeded') return
    this.stopSound()
    if (obj.url) delete obj.url
    currSound.play(obj)
    currSound.on('complete', handleComplete)
  },
  pausedSound (soundID, param, callBack) {
    window.createjs.source[soundID].paused = !window.createjs.source[soundID].paused
  },
  stopSound (soundID) {
    soundID ? window.createjs.musicSource[soundID].stop() : window.createjs.Sound.stop()
  }
}
function mergeObj (url, id) { // 复制对象
  for (let i in window.createjs.musicSource) {
    if (url === window.createjs.musicSource[i].src) {
      window.createjs.musicSource[id] = window.createjs.musicSource[i]
    }
  }
}
function handleComplete () {
  soundMessage._this.$store.state.musicLoading = false
}
function loadHandler (event) {
  soundMessage.myInstance = window.createjs.musicSource[event.id] = window.createjs.Sound.createInstance(event.id)
  soundMessage.stopSound()
  if (soundMessage.musicObj.url) delete soundMessage.musicObj.url
  window.createjs.musicSource[event.id].play(soundMessage.musicObj)
  sounxdMessage.musicObj = ''
  soundMessage.myInstance.on('complete', handleComplete)
}

let imgHostUrl = 'https://static.xaqlt.com/'
export function imgHost (val) {
  return imgHostUrl + val
}
export function imgAvator (val) {
  return imgHostUrl + '/imgs/userprofile/' + val
}
